package com.eleven.trades.controller;


import com.alibaba.fastjson.JSONObject;
import com.eleven.trades.entity.Flow;
import com.eleven.trades.error.BusinessException;
import com.eleven.trades.mapper.AccountMapper;
import com.eleven.trades.result.JsonResult;
import com.eleven.trades.result.JsonResultFactory;
import com.eleven.trades.result.ResultCode;
import com.eleven.trades.serverimpl.AccountServerImpl;
import com.eleven.trades.serverimpl.FlowServerImpl;
import com.eleven.trades.vo.FlowRequest;
import com.eleven.trades.vo.FlowResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linhaizeng
 */
@RestController
public class FlowController {
    @Autowired
    AccountServerImpl accountServer;

    @Autowired
    FlowServerImpl flowServer;

    // TODO: 2021/7/16

    @Autowired
    AccountMapper accountMapper;

    @PostMapping("/trans")
    public JsonResult trans( @RequestBody  FlowRequest flowRequest) throws BusinessException {
        // 获取前端输入数据
        String nameSrc = flowRequest.getNameSrc();
        String nameDest = flowRequest.getNameDest();
        String accountSrc = flowRequest.getAccountSrc();
        String accountDest = flowRequest.getAccountDest();
        Double balance = flowRequest.getBalance();
        // 校验姓名与账户
        if (!accountServer.signAccount(nameDest,accountDest)) {
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.USER_ACCOUNT_DISABLE);
        }
        if (!accountServer.signAccount(nameSrc,accountSrc)) {
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.USER_ACCOUNT_DISABLE);
        }
        // 转账
        accountServer.signAccount(nameSrc,accountSrc);
        String transfer = accountServer.transfer(accountSrc, accountDest, balance);

        if (ResultCode.SUCCESS.getMessage().equals(transfer)){
            return JsonResultFactory.getInstance().buildSuccessResult();
        } else {
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.COMMON_FAIL);
        }
    }

    @GetMapping("/select_flow")
    public JsonResult selectFlows(@RequestParam("account") String account) {
        try {
            List<Flow> flows = flowServer.selectFlow(account);
            List<FlowResponse> res = new ArrayList<>();
            flows.forEach( e-> res.add(new FlowResponse(e,e.getOutId().equals(account.toString()))));
            String s = JSONObject.toJSONString(res);
            return JsonResultFactory.getInstance().buildSuccessResult(s);
        } catch (Exception e){
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.COMMON_FAIL);
        }
    }

    @GetMapping("/select_two")
    public JsonResult selectTransFlow(@RequestParam("account_in") String account_in,
                                      @RequestParam("account_out") String account_out){
        /**
         * 根据输入账户与输出账户 查询交易来往
         */
        try {
            List<Flow> flows = flowServer.selectTransFlow(account_in, account_out);
            List<FlowResponse> res = new ArrayList<>();
            // 查询转出姓名 与 转入姓名
            String nameIn = accountMapper.selectName(account_in);
            String nameOut = accountMapper.selectName(account_out);

            flows.forEach( e-> res.add(new FlowResponse(e, nameIn,nameOut,e.getOutId().equals(account_in))));

            String s = JSONObject.toJSONString(res);
            return JsonResultFactory.getInstance().buildSuccessResult(s);
        } catch (Exception e){
            return JsonResultFactory.getInstance().buildFailResult(ResultCode.COMMON_FAIL);
        }
    }


}
